import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';

import { FoodOrder } from '../../domain/foodOrder';
import { User } from '../../domain/user';
import { Food } from '../../domain/food';

import { FoodOrderService } from '../../service/food-order.service';
import { UserService } from '../../service/user.service';
import { FoodService } from '../../service/food.service';

declare var layui: any;

@Component({
  selector: 'app-user-food-list',
  templateUrl: './user-food-list.component.html',
  styleUrls: ['./user-food-list.component.css'],
  providers: [FoodOrderService, UserService, FoodService]
})
export class UserFoodListComponent implements OnInit {

  foods: Food[];  //食物

  foodOrder: FoodOrder;  //食物订单
  foodOrders: FoodOrder[];  //所有食物订单

  orderFood: FoodOrder; //用户订食物

  user: User;  //用户

  foodId: string;

  constructor(
    private foodOrderService: FoodOrderService,
    private foodService: FoodService,
    private router: Router,
    private userService: UserService,
  ) { }

  ngOnInit() {

    this.userService.getUserById(<number><any>localStorage.getItem('userId'))
      .then(res => {
        this.user = res;
        this.foodOrderService.getOrderFoods()
        .then(ress => {
          this.foodOrders = ress;
          this.foodService.getFoods()
          .then(resss => this.foods = resss);
        });
      });

    this.orderFood = new FoodOrder();
  }

  createFoodOrder(): void {
    this.foodService.getFoodById(<number><any>this.foodId)
      .then(res => {
        this.orderFood.foodId = res.name;
        this.orderFood.roomId = this.user.room;
        this.orderFood.status = "WAIT";
        this.foodOrderService.createOrderFood(this.orderFood);
      });
  }

  complateFoodOrder(order:FoodOrder): void{
    order.status="COMPLATE";
    this.foodOrderService.updateOrderFood(order);
  }


  //重置
  reset(): void {
    location.reload();
  }
}
